package com.obas.integration.element.telnet.loader;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;

import com.ease.common.util.FileUtil;
import com.ease.common.util.OutputUtil;
import com.ease.common.vo.editor.AbstractObjectEditor;
import com.ease.integration.telent.TelnetClient;
import com.ease.integration.telent.TelnetExecutionCallback;
import com.obas.integration.element.telnet.editor.HWS9303TelnetEditor;
import com.obas.model.business.DeviceMacAddress;

public class HWS9303TelnetDeviceMacAddressLoader extends HWDefaultTelnetDeviceMacAddressLoader {

	public AbstractObjectEditor<DeviceMacAddress> getRawEditor() {
		return new HWS9303TelnetEditor();
	}

	public List<DeviceMacAddress> editTextAsObject(String rawDeviceMacAddress) {
		AbstractObjectEditor<DeviceMacAddress> rawEditor = this.getRawEditor();

		rawDeviceMacAddress = StringUtils.replace(rawDeviceMacAddress, "[42D", "\r\n");
		String[] rows = StringUtils.delimitedListToStringArray(rawDeviceMacAddress, "\r\n");
		if (notEmpty(rows)) {
			List<DeviceMacAddress> deviceMacAddresses = new ArrayList<DeviceMacAddress>();
			boolean start = false;
			String slotCode = null;
			for (String row : rows) {

				if (!notEmpty(row)) {
					continue;
				}
				if ("-------------------------------------------------------------------------------".equals(row)) {
					continue;
				}
				if ("               VSI/SI                                              MAC-Tunnel  ".equals(row)) {
					start = true;
					continue;
				}
				if (row.startsWith("  ---- More ----")) {
					continue;
				}
				if (row.startsWith("Total matching")) {
					start = false;
					continue;
				}
				if (row.startsWith("MAC address table of")) {
					slotCode = row.substring("MAC address table of slot ".length(), row.length() - 1);
					continue;
				}

				if (row.length() <= 70) {
					continue;
				}
				if (true == start) {
					rawEditor.setAsText(row);
					DeviceMacAddress deviceMacAddress = rawEditor.getObject();
					if (null != deviceMacAddress) {
						deviceMacAddress.setSlotCode(slotCode);
						deviceMacAddresses.add(deviceMacAddress);
					}
				}
			}
			return escapeEmpty(deviceMacAddresses);
		}
		return null;
	}

	public String doLoadRawDeviceMacAddress() {

		return this.execute(new TelnetExecutionCallback<String>() {

			public String execute(TelnetClient telnetClient) {

				telnetClient.request(command);
				return telnetClient.response(null);
			}

		});

	}

	public static void main1(String[] args) {

		HWS9303TelnetDeviceMacAddressLoader retriever = new HWS9303TelnetDeviceMacAddressLoader();

		retriever.setHost("192.168.239.3");
		retriever.setPort(23);
		retriever.setUsername("huawei");
		retriever.setPassword("huawei");

		retriever.setUsernamePrompt("Username:");
		retriever.setPasswordPrompt("Password:");
		retriever.setMorePrompt("---- More ----");
		retriever.setCommand("disp mac-address");

		List<DeviceMacAddress> deviceMacAddresses = retriever.loadDeviceMacAddress();

		OutputUtil.p(deviceMacAddresses);

	}

	public static void main(String[] args) {
		String rawDeviceMacAddress = FileUtil.readClassPathResourceToString(HWDefaultTelnetDeviceMacAddressLoader.class, "HW S9303.txt");
		List<DeviceMacAddress> deviceMacAddresses = new HWS9303TelnetDeviceMacAddressLoader().editTextAsObject(rawDeviceMacAddress);
		OutputUtil.p(deviceMacAddresses);
	}
}
